package com.ui;

import java.awt.EventQueue;
import java.io.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Vector;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTable;
import javax.swing.ImageIcon;
import com.controlador.JavaExcelWriter;
import com.controlador.JavaPDFWriter;
import com.util.Constantes;

public class ResultadoBusqueda extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTable table;
	String[][] datos = null;
	String[] encabezado = null;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ResultadoBusqueda frame = new ResultadoBusqueda();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ResultadoBusqueda() {
		setTitle("Search Result");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 600, 380);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JButton btnOk = new JButton("Ok");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		
		JScrollPane scrollPane = new JScrollPane();
		
		JButton pdf = new JButton("");
		pdf.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				DefaultTableModel m = (DefaultTableModel) table.getModel();
				Vector<?> vm = m.getDataVector();
				new JavaPDFWriter().escribePDF(getEncabezado(),vm);
				//mandar abrir el archivo
				try 
				{ 
					String rutaAbsoluta = new File(Constantes.ARCHIVO_PDF_TEMPORAL).getAbsolutePath();
					@SuppressWarnings("unused")
					Process p = Runtime.getRuntime().exec ("rundll32 SHELL32.DLL,ShellExec_RunDLL "+ rutaAbsoluta ); 	
				} 
				catch (Exception e) 
				{ 
					System.err.println("Error: "+e.getMessage()); 
				}
				
			}
		});
		pdf.setIcon(new ImageIcon(ResultadoBusqueda.class.getResource("/com/util/img/pdf-ico.png")));
		
		JButton excel = new JButton("");
		excel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				DefaultTableModel m = (DefaultTableModel) table.getModel();
				Vector<?> vm = m.getDataVector();
				new JavaExcelWriter().escribeExcel(vm,getEncabezado());
				
				//mandar abrir el archivo
				try 
				{ 
					String rutaAbsoluta = new File(Constantes.ARCHIVO_EXCEL_TEMPORAL).getAbsolutePath();
					@SuppressWarnings("unused")
					Process p = Runtime.getRuntime().exec ("rundll32 SHELL32.DLL,ShellExec_RunDLL "+ rutaAbsoluta ); 
					
				} 
				catch (Exception ex) 
				{ 
					System.err.println("Error: "+ex.getMessage()); 
				}
				
			}
		});
		excel.setIcon(new ImageIcon(ResultadoBusqueda.class.getResource("/com/util/img/excel-ico.png")));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addComponent(scrollPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 564, Short.MAX_VALUE)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addContainerGap(519, Short.MAX_VALUE)
							.addComponent(btnOk)))
					.addContainerGap())
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap(381, Short.MAX_VALUE)
					.addComponent(pdf)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(excel)
					.addGap(9))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addComponent(pdf)
						.addComponent(excel))
					.addPreferredGap(ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 219, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(btnOk)
					.addContainerGap())
		);
		table = new JTable();
		scrollPane.setViewportView(table);
		contentPane.setLayout(gl_contentPane);
	}
	
	public void populateInfo()
	{
		DefaultTableModel m = (DefaultTableModel) table.getModel();
		m.setDataVector(datos, encabezado);
	}

	public String[][] getDatos() {
		return datos;
	}

	public void setDatos(String[][] datos) {
		this.datos = datos;
	}

	public String[] getEncabezado() {
		return encabezado;
	}

	public void setEncabezado(String[] encabezado) {
		this.encabezado = encabezado;
	}
}
